//
//  MMFile.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 8/3/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMFile.h"

@implementation MMFile

- (id)initWithPath:(NSString *)path
{
    if (self = [super init]) {
        self.filePath = path;
        self.fileName = [path lastPathComponent];
        
        NSFileManager *fm = [NSFileManager defaultManager];
        NSDictionary *attributes = [fm attributesOfItemAtPath:path error:nil];
        if (attributes) {
            NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"ddMMyyyy" options:0
                                                                      locale:[NSLocale currentLocale]];
            NSDateFormatter *fm = [[NSDateFormatter alloc] init];
            [fm setDateFormat:formatString];
            NSString *strDate = [fm stringFromDate:[attributes fileModificationDate]];
            self.fileModifiedDate = strDate;
            self.fileSize  = [attributes fileSize];
            self.dateModifed = [self dateWithOutTime:[attributes fileModificationDate]];
        }
    }
    return self;
}

-(NSDate *)dateWithOutTime:(NSDate *)datDate{
    if( datDate == nil ) {
        datDate = [NSDate date];
    }
    NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
    return [[NSCalendar currentCalendar] dateFromComponents:comps];
}

@end
